package com.chaodev.base.utils;

import cn.hutool.extra.qrcode.QrCodeException;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;

@Service
public class QrCodeGenerate {

    @Autowired
    private QrConfig config;

    //生成到流
    public void createCodeToStream(String content, HttpServletResponse response) {
        try {
            QrCodeUtil.generate(content,config, "png", response.getOutputStream());
        } catch (QrCodeException | IOException e) {
            e.printStackTrace();
        }
    }

    //生成到MultipartFile
    public MultipartFile createCodeToMultipartFile(String content,String qrName) {
        MultipartFile multipartFile = null;
        try {
            BufferedImage image = QrCodeUtil.generate(content, config);
            //BufferedImage 转化为 ByteArrayOutputStream
            //将newImage写入字节数组输出流
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write( image, "png", baos );

            //转换为MultipartFile
            multipartFile = new MultipartFileDto("file",qrName+".png","png",baos.toByteArray());

        } catch (QrCodeException | IOException e) {
            e.printStackTrace();
        }finally {
            return multipartFile;
        }
    }


    public class MultipartFileDto implements MultipartFile {
        private final String name;

        private String originalFilename;

        private String contentType;

        private final byte[] content;

        /**
         * Create a new MultipartFileDto with the given content.
         * @param name the name of the file
         * @param content the content of the file
         */
        public MultipartFileDto(String name, byte[] content) {
            this(name, "", null, content);
        }

        /**
         * Create a new MultipartFileDto with the given content.
         * @param name the name of the file
         * @param contentStream the content of the file as stream
         * @throws IOException if reading from the stream failed
         */
        public MultipartFileDto(String name, InputStream contentStream) throws IOException {
            this(name, "", null, FileCopyUtils.copyToByteArray(contentStream));
        }

        /**
         * Create a new MultipartFileDto with the given content.
         * @param name the name of the file
         * @param originalFilename the original filename (as on the client's machine)
         * @param contentType the content type (if known)
         * @param content the content of the file
         */
        public MultipartFileDto(String name, String originalFilename, String contentType, byte[] content) {
            this.name = name;
            this.originalFilename = (originalFilename != null ? originalFilename : "");
            this.contentType = contentType;
            this.content = (content != null ? content : new byte[0]);
        }

        /**
         * Create a new MultipartFileDto with the given content.
         * @param name the name of the file
         * @param originalFilename the original filename (as on the client's machine)
         * @param contentType the content type (if known)
         * @param contentStream the content of the file as stream
         * @throws IOException if reading from the stream failed
         */
        public MultipartFileDto(String name, String originalFilename, String contentType, InputStream contentStream)
                throws IOException {

            this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
        }

        @Override
        public String getName() {
            return this.name;
        }

        @Override
        public String getOriginalFilename() {
            return this.originalFilename;
        }

        @Override
        public String getContentType() {
            return this.contentType;
        }

        @Override
        public boolean isEmpty() {
            return (this.content.length == 0);
        }

        @Override
        public long getSize() {
            return this.content.length;
        }

        @Override
        public byte[] getBytes() throws IOException {
            return this.content;
        }

        @Override
        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(this.content);
        }

        @Override
        public void transferTo(File dest) throws IOException, IllegalStateException {
            FileCopyUtils.copy(this.content, dest);
        }

    }
}
